package com.baitiaojun.moments.handler;

import com.baitiaojun.api.exception.ConditionException;
import com.baitiaojun.api.vo.JsonRes;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @类名 GlobalExceptionHandler
 * @作者 白条君
 * @创建日期 2022/10/25 18:15
 * @版本 1.0
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public JsonRes exceptionHandler(Exception e) {
        String message = e.getMessage();
        if (e instanceof ConditionException) {
            int code = ((ConditionException) e).getCode();
            return JsonRes.builder().error(code, message);
        } else {
            return JsonRes.builder().error(HttpStatus.INTERNAL_SERVER_ERROR.value(), message);
        }
    }
}